﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;

namespace GG.Data
{
    public class MyData
    {

        private string _conStringLog = ConfigurationManager.ConnectionStrings["GG_EmailLog_DBConnectionString"].ToString();
        private string _conString = ConfigurationManager.ConnectionStrings["GG_DBConnectionString"].ToString();

        public MyData()
        {
        }

        public DataSet GetApplicationStatusList()
        {
            SqlCommand _sqlCmd = null;
            SqlConnection _sqlConn = new SqlConnection();
            DataSet _dsApplicationStatus = new DataSet();

            try
            {
                _sqlConn.ConnectionString = _conString;

                DateTime d = GG.Global.General.GetUTCDateTime();
                DateTime _dtmstartApplicationDate = GG.Global.General.GetUTCDateTime();
                DateTime _dtmendApplicationDate = d;
                //if (d.DayOfWeek == DayOfWeek.Monday)
                //{
                //    // _dtmstartApplicationDate = d.AddDays(-3);
                //    DateTime tempD;
                //    for (int i = -1; i >= -3; i--)
                //    {
                //        tempD = d.AddDays(i);
                //        _dtmstartApplicationDate = tempD;
                //        if (tempD.Month != d.Month)
                //        {
                //            break;
                //        }
                //    }
                //}
                //else
                //{
                _dtmstartApplicationDate = d.AddDays(-1);
                //}

                string _strstartApplicationDate = "";
                string _strendApplicationDate = "";
                string IsDate = ConfigurationManager.AppSettings["IsDate"].ToString();
                if (IsDate.ToString() != string.Empty && IsDate == "1")
                {
                    _strstartApplicationDate = ConfigurationManager.AppSettings["FromDate"].ToString();
                    _strendApplicationDate = ConfigurationManager.AppSettings["ToDate"].ToString();
                }
                else
                {
                    _strstartApplicationDate = _dtmstartApplicationDate.Day.ToString("00") + "/" + _dtmstartApplicationDate.Month.ToString("00") + "/" + _dtmstartApplicationDate.Year.ToString("0000");
                    _strendApplicationDate = _dtmendApplicationDate.Day.ToString("00") + "/" + _dtmendApplicationDate.Month.ToString("00") + "/" + _dtmendApplicationDate.Year.ToString("0000");
                }

                //_strstartApplicationDate = "17/02/2014";
                //_strendApplicationDate = "18/02/2014";

                _sqlConn.Open();
                _sqlCmd = new SqlCommand();
                _sqlCmd.Connection = _sqlConn;
                _sqlCmd.CommandType = CommandType.StoredProcedure;
                _sqlCmd.CommandText = "sprocCXTHApplicationStatusReportSelectedList";
                SqlParameter _sqlPara1 = new SqlParameter();
                _sqlPara1.ParameterName = "@ApplicationStartDate";
                _sqlCmd.Parameters.Add(_sqlPara1);
                _sqlCmd.Parameters[0].Value = _strstartApplicationDate;

                SqlParameter _sqlPara2 = new SqlParameter();
                _sqlPara2.ParameterName = "@ApplicationEndDate";
                _sqlCmd.Parameters.Add(_sqlPara2);
                _sqlCmd.Parameters[1].Value = _strendApplicationDate;

                _sqlCmd.CommandTimeout = 60;
                SqlDataAdapter _sqlDa = new SqlDataAdapter(_sqlCmd);
                _sqlDa.Fill(_dsApplicationStatus);


            }
            catch (Exception Ex)
            {
                Ex.ToString();

            }
            finally
            {
                if (_sqlCmd != null)
                {
                    _sqlCmd = null;
                }

                if (_sqlConn.State != ConnectionState.Closed)
                {
                    _sqlConn.Close();
                    _sqlConn = null;
                }

            }


            return _dsApplicationStatus;
        }

        public void InsertMailCountLog(string applicationName)
        {
            SqlCommand _sqlCmd = null;
            SqlConnection _sqlConn = new SqlConnection();
            DataSet _dsOffLineQuotes = new DataSet();
            try
            {
                string islive = ConfigurationManager.AppSettings["islive"].ToString();
                _sqlConn.ConnectionString = _conStringLog;
                _sqlConn.Open();

                _sqlCmd = new SqlCommand();
                _sqlCmd.Connection = _sqlConn;
                _sqlCmd.CommandType = CommandType.StoredProcedure;
                _sqlCmd.CommandText = "sproctblGGEmailsLogInsertSingleItem";

                SqlParameter _sqlPara1 = new SqlParameter();
                _sqlPara1.ParameterName = "@IsLive";
                _sqlCmd.Parameters.Add(_sqlPara1);
                _sqlCmd.Parameters[0].Value = islive;

                SqlParameter _sqlPara2 = new SqlParameter();
                _sqlPara2.ParameterName = "@ApplicationName";
                _sqlCmd.Parameters.Add(_sqlPara2);
                _sqlCmd.Parameters[1].Value = applicationName;

                SqlParameter _sqlPara3 = new SqlParameter();
                _sqlPara3.ParameterName = "@EmailCount";
                _sqlCmd.Parameters.Add(_sqlPara3);
                _sqlCmd.Parameters[2].Value = 1;

                _sqlCmd.ExecuteNonQuery();
            }
            catch (Exception Ex)
            {
            }
            finally
            {
                if (_sqlCmd != null)
                {
                    _sqlCmd = null;
                }

                if (_sqlConn.State != ConnectionState.Closed)
                {
                    _sqlConn.Close();
                    _sqlConn = null;
                }
            }
        }

    }
}
